﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils;

namespace UI.Desktop.Planes
{
    public partial class ABM : Form
    {
        private Logic.Planes _hnd_planes = new Logic.Planes();
        private Logic.Carreras _hnd_carreras = new Logic.Carreras();

        private Logic.Planes HndPlanes
        {
            get { return _hnd_planes; }
        }
        private Logic.Carreras HndCarreras
        {
            get { return _hnd_carreras; }
        }

        public ABM()
        {
            InitializeComponent();
            LoadList();
        }

        private void LoadList()
        {
            int colour = 0;
            lvLista.BeginUpdate();
            lvLista.Items.Clear();
            HndPlanes.GetCollection().ForEach(c =>
            {
                ListViewItem i = new ListViewItem(new string[]{
                    "",
                    c.ID.ToString(),
                    c.Nombre,
                    HndCarreras.GetByID(c.IDCarrera).Nombre
                });
                i.BackColor = (colour++ % 2 == 0 ? Properties.Settings.Default.AlternateColour : Color.White);
                lvLista.Items.Add(i);
            });
            lvLista.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
            lvLista.EndUpdate();
        }

        private void lvLista_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            btnEditar.Enabled = (lvLista.CheckedItems.Count == 1);
            btnEliminar.Enabled = (lvLista.CheckedItems.Count > 0);
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            using (Register r = new Register())
            {
                if (r.ShowDialog() == DialogResult.OK) LoadList();
            }
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            if (lvLista.CheckedItems.Count == 1)
            {
                Entities.Plan instance = HndPlanes.GetByID(lvLista.CheckedItems[0].SubItems[1].Text.ToInt32());
                using (Register r = new Register(instance))
                {
                    if (r.ShowDialog() == DialogResult.OK) LoadList();
                }
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (lvLista.CheckedItems.Count > 0)
            {
                if (DialogResult.Yes == CommonControls.WarnBox(Text, "Una vez eliminados, los registros no podrán recuperarse.\n¿Está seguro de que desea continuar?"))
                {
                    foreach (ListViewItem i in lvLista.CheckedItems)
                    {
                        Entities.Plan instance = HndPlanes.GetByID(i.SubItems[1].Text.ToInt32());
                        instance.State = Commons.EntityState.Deleted;
                        HndPlanes.Delete(instance);
                    }
                    CommonControls.InformBox(Text, "Los registros previamente seleccionados fueron eliminados exitosamente.");
                    LoadList();
                }
            }
        }
    }
}
